package com.zz.ecommerce.declareManage.exportB2CDeclare.cancelBillManage;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.ECancelConsumptionDao;
import com.zz.ecommerce.pojo.TECancelConsumption;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

/**
 * Created by steven on 2016/3/30.
 */
@Controller
@RequestMapping("/exportB2C/cancelBillConsumption")
public class ECancelConsumprionController {

    @Autowired
    private ECancelConsumptionDao cancelConsumptionDao;

    /**
     * 查询核销单关联的单损耗信息列表
     *
     * @param relateId 关联核销单唯一索引
     * @param request
     * @return
     */
    @RequestMapping("/queryConsumptionList")
    @ResponseBody
    public GridResult queryConsumptionList(@RequestParam(value = "relateId") String relateId, GridRequest request) {

        if(StringUtils.isEmpty(relateId)){
            return new GridResult<>(null, request, 0);
        }

        TECancelConsumption query = new TECancelConsumption();
        query.setRelateId(relateId);
        long count = cancelConsumptionDao.count(TableConstants.T_E_CANCEL_CONSUMPTION, query);
        List<TECancelConsumption> list = cancelConsumptionDao.page(TableConstants.T_E_CANCEL_CONSUMPTION, query, request.getStart(),
                request.getEnd(), request.getSortOrder());

        return new GridResult<>(list, request, count);
    }

    /**
     * 删除核销单关联的单损耗单信息
     *
     * @param id 单损耗单信息唯一索引
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteConsumption(@RequestParam(value = "id") String id) {
        int count = cancelConsumptionDao.delete(TableConstants.T_E_CANCEL_CONSUMPTION, " WHERE ID = '" + id + "'");
        return count > 0;
    }

    /**
     * 新增核销单关联的单损耗单信息
     *
     * @param consumption 单损耗单信息
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public boolean saveConsumption(TECancelConsumption consumption) {
        int count;
        if(StringUtils.isNotEmpty(consumption.getId())){
            TECancelConsumption query = new TECancelConsumption();
            query.setId(consumption.getId());
            TECancelConsumption consumption1 = cancelConsumptionDao.query(TableConstants.T_E_CANCEL_CONSUMPTION, query);
            if(null != consumption1){
                count = cancelConsumptionDao.update(TableConstants.T_E_CANCEL_CONSUMPTION,consumption," WHERE ID = #{updateObj.id}");
            }else{
                count = cancelConsumptionDao.insert(TableConstants.T_E_CANCEL_CONSUMPTION,consumption);
            }
        }else{
            consumption.setId(UUID.randomUUID().toString());
            count = cancelConsumptionDao.insert(TableConstants.T_E_CANCEL_CONSUMPTION,consumption);
        }

        return count > 0;
    }
}
